
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>Homebrew是如何工作的 | SL&#39;s Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="。Homebrew 是 OSX 平台下一个很好的包管理器。如果是命令行玩家，或者用过 Linux 之类的，对 apt、yum 等也不陌生，homebrew 能够极大的简化我们安装三方工具的步骤，高效便捷。当然 Homebrew 还不止步于命令行工具，通过homebrew-cask ，甚至可以将包管理拓展到 GUI 应用，比如我需要安装一个新的 chome，直接 cask install chrom">
<meta name="keywords" content="OSX">
<meta property="og:type" content="article">
<meta property="og:title" content="Homebrew是如何工作的">
<meta property="og:url" content="http://caltyang.cn/2016/04/29/Homebrew是如何工作的/index.html">
<meta property="og:site_name" content="SL&#39;s Blog">
<meta property="og:description" content="。Homebrew 是 OSX 平台下一个很好的包管理器。如果是命令行玩家，或者用过 Linux 之类的，对 apt、yum 等也不陌生，homebrew 能够极大的简化我们安装三方工具的步骤，高效便捷。当然 Homebrew 还不止步于命令行工具，通过homebrew-cask ，甚至可以将包管理拓展到 GUI 应用，比如我需要安装一个新的 chome，直接 cask install chrom">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://slblogimg.oss-cn-beijing.aliyuncs.com/images/20160429/brew_info_cloc.png">
<meta property="og:image" content="http://slblogimg.oss-cn-beijing.aliyuncs.com/images/20160429/cakebrew.png">
<meta property="og:updated_time" content="2018-10-23T09:10:39.504Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Homebrew是如何工作的">
<meta name="twitter:description" content="。Homebrew 是 OSX 平台下一个很好的包管理器。如果是命令行玩家，或者用过 Linux 之类的，对 apt、yum 等也不陌生，homebrew 能够极大的简化我们安装三方工具的步骤，高效便捷。当然 Homebrew 还不止步于命令行工具，通过homebrew-cask ，甚至可以将包管理拓展到 GUI 应用，比如我需要安装一个新的 chome，直接 cask install chrom">
<meta name="twitter:image" content="http://slblogimg.oss-cn-beijing.aliyuncs.com/images/20160429/brew_info_cloc.png">
  
  
    <link rel="icon" href="/favicon.png">
  
  <link rel="stylesheet" href="/css/style.css">
  <!--[if lt IE 9]><script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7/html5shiv.min.js"></script><![endif]-->
  
</head>
<body>
<div id="container">
  <div id="wrap">
    <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">SL&#39;s Blog</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//www.baidu.com/baidu" method="get" accept-charset="utf-8" class="search-form">
          <input type="search" name="word" maxlength="20" class="search-form-input" placeholder="Search">
          <input type="submit" value="" class="search-form-submit">
          <input name=tn type=hidden value="bds">
          <input name=cl type=hidden value="3">
          <input name=ct type=hidden value="2097152">
          <input type="hidden" name="si" value="caltyang.cn">
        </form>
      </div>
    </div>
  </div>
</header>
    <div class="outer">
      <section id="main"><article id="post-Homebrew是如何工作的" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2016/04/29/Homebrew是如何工作的/" class="article-date">
  <time datetime="2016-04-29T11:28:55.000Z" itemprop="datePublished">2016-04-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Homebrew是如何工作的
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>。Homebrew 是 OSX 平台下一个很好的包管理器<br>。如果是命令行玩家，或者用过 Linux 之类的，对 apt、yum 等也不陌生，homebrew 能够极大的简化我们安装三方工具的步骤，高效便捷<br>。当然 Homebrew 还不止步于命令行工具，通过homebrew-cask ，甚至可以将包管理拓展到 GUI 应用，比如我需要安装一个新的 chome，直接 cask install chrome 即可，科技范十足<br>。本文记录 Homebrew 的一些概念，工作方式等等<br><a id="more"></a></p>
<h2 id="0x01-几个-homebew-的命令"><a href="#0x01-几个-homebew-的命令" class="headerlink" title="0x01 几个 homebew 的命令"></a>0x01 几个 homebew 的命令</h2><hr>
<p>1.brew install/remove</p>
<blockquote>
<p>。安装和卸载 package，和 apt-get install/uninstall 类似</p>
</blockquote>
<p>2.brew info/desc</p>
<blockquote>
<p>。查看一个 package 的基本描述和详细信息</p>
</blockquote>
<p>3.brew doctor</p>
<blockquote>
<p>。检查，比如package是不是最新，软连接是否正确，有没有一些配置的问题之类</p>
</blockquote>
<p>4.brew link/unlink</p>
<blockquote>
<p>。用来重新构建一个命令的软连接和取消软连接</p>
</blockquote>
<p>5.brew update 和 brew upgrade</p>
<blockquote>
<p>。update 是更新 homebrew 的 formula 表，其实就是进行一个 pull request，和远程仓库进行统一<br>。upgrade 则是实际的进行 package 的更新</p>
</blockquote>
<h2 id="0x02-应用是如何安装的"><a href="#0x02-应用是如何安装的" class="headerlink" title="0x02 应用是如何安装的"></a>0x02 应用是如何安装的</h2><hr>
<p>。我们知道 brew 安装的软件默认都在 /usr/local/Cellar 文件夹下<br>。而其实 homebrew 的安装目录也是在 /usr/local 下的，homebrew 会在该目录下初始 git 环境，然后在该目录下维护和track包之类，查看该目录下的 git 配置可发现</p>
<p>。默认安装的 Formula 在 /usr/local/Library/Formula 文件夹下<br>。我们已 cloc（一个代码统计的工具）为例，它的formula如下：<br><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Cloc</span> &lt; Formula</span></span><br><span class="line">  desc <span class="string">"Statistics utility to count lines of code"</span></span><br><span class="line">  homepage <span class="string">"https://github.com/AlDanial/cloc/"</span></span><br><span class="line">  url <span class="string">"https://github.com/AlDanial/cloc/archive/v1.66.tar.gz"</span></span><br><span class="line">  sha256 <span class="string">"6d8b5e9fac8934478a0fd815f60d814b93d4a12344c4820f5b47a55048b21d53"</span></span><br><span class="line">  head <span class="string">"https://github.com/AlDanial/cloc.git"</span></span><br><span class="line">  bottle <span class="keyword">do</span></span><br><span class="line">    cellar <span class="symbol">:any_skip_relocation</span></span><br><span class="line">    sha256 <span class="string">"c140cd2b7244b48e45971130d69ffa3a250d7fda4885098b6a01a33fb3bc4ef1"</span> =&gt; <span class="symbol">:el_capitan</span></span><br><span class="line">    sha256 <span class="string">"4abbd7aec11e52eac56a1ab969a3825ff162f7d19598b94c02116dc01ad18e43"</span> =&gt; <span class="symbol">:yosemite</span></span><br><span class="line">    sha256 <span class="string">"994e73fb9afde00c2f3fde26a9421707bf73ce85de6d7021d4db2819307fbd48"</span> =&gt; <span class="symbol">:mavericks</span></span><br><span class="line">  <span class="keyword">end</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">install</span></span></span><br><span class="line">    system <span class="string">"make"</span>, <span class="string">"-C"</span>, <span class="string">"Unix"</span>, <span class="string">"prefix=<span class="subst">#&#123;prefix&#125;</span>"</span>, <span class="string">"install"</span></span><br><span class="line">  <span class="keyword">end</span></span><br><span class="line">  test <span class="keyword">do</span></span><br><span class="line">    (testpath/<span class="string">"test.c"</span>).write &lt;&lt;-EOS.undent</span><br><span class="line">      <span class="comment">#include &lt;stdio.h&gt;</span></span><br><span class="line">      int main(void) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    EOS</span><br><span class="line">    assert_match <span class="string">"1,C,0,0,4"</span>, shell_output(<span class="string">"<span class="subst">#&#123;bin&#125;</span>/cloc --csv ."</span>)</span><br><span class="line">  <span class="keyword">end</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure></p>
<p>。细心的同学可能就发现了，这里的一些信息其实和 brew desc 和 brew info 时是一致的，比如：<br><img src="http://slblogimg.oss-cn-beijing.aliyuncs.com/images/20160429/brew_info_cloc.png" alt=""><br>。所以，当我们安装 cloc （通过 brew install cloc）时，首先，homebrew 会使用 curl 来获取文件，并放在缓存目录中<br>。然后出于安全等因素考虑，会计算 checksum，并和sha256那个作比对<br>。如果文件下载正确的话，接下啦 homebrew 会解压归档到一个临时文件夹，然后执行 install 命令，比如这里就是下面的命令<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">make -C Unix prefix=/usr/local/Cellar/xxx install</span><br></pre></td></tr></table></figure></p>
<p>。当安装顺利，homebrew 会建立一个 /usr/local/cellar/xxx/bin 下命令的软连接到 /usr/local/bin 以确保你可以直接在命令行中执行该命令<br>。当然，同样会建立到 lib 和 man 的软连接<br>。安装完成后，你也可以使用 brew test 命令来测试安装<br>。test 命令会建立一个临时文件夹，然后建立一个 sandbox，之后执行 formula 中的test方法</p>
<h2 id="0x03-几个模糊的概念"><a href="#0x03-几个模糊的概念" class="headerlink" title="0x03 几个模糊的概念"></a>0x03 几个模糊的概念</h2><hr>
<p>1.brew tap</p>
<blockquote>
<p>。这个其实类似 Debian 系列 APT 中的 PPA<br>。可以用来拓展 Homebrew 的仓库，使其覆盖更多的应用<br>。直接输入 brew tap 时，我们就可以看到本地 clone 了几个仓库<br>。参考： github-brew tap</p>
</blockquote>
<p>2.formula</p>
<blockquote>
<p>。简单的理解就是上面的 rb 文件<br>。用来定义安装，测试，定义应用的信息等</p>
</blockquote>
<p>3.bottles</p>
<blockquote>
<p>。内建的 formula<br>。在 homebrew 服务器上以 tag.gz 的形式存在</p>
</blockquote>
<p>4.DSL</p>
<blockquote>
<p>。DSL 是特定领域语言的缩写，一个简单的理解就是不想Java、C之类通用语言一样，DSL目标是实现特定的功能，比如包的管理，所以不必像其他领域一样，考虑太多的通用性和复用性，DSL中往往有很多扩展的语法、命令之类，以实现特定的功能<br>。比如 Gradle 中可以使用 Groovy 来实现 DSL</p>
</blockquote>
<h2 id="Homebrew-cask"><a href="#Homebrew-cask" class="headerlink" title="Homebrew cask"></a>Homebrew cask</h2><hr>
<p>。参考：<a href="http://ksmx.me/homebrew-cask-cli-workflow-to-install-mac-applications/" target="_blank" rel="noopener">简洁优雅的Mac OS X软件安装体验 - homebrew-cask</a></p>
<h2 id="homebrew-service"><a href="#homebrew-service" class="headerlink" title="homebrew service"></a>homebrew service</h2><hr>
<p>。我们知道有些 Linux 中可以通过 service 命令来开启关闭一些服务，比如 Nginx，MySQL 之类<br>。OSX 中也可以，不过是使用 launchctl<br>。Homebrew service 其实就是意在提供一个统一的 service 方式来管理服务<br>。参考：<a href="https://github.com/Homebrew/homebrew-services" target="_blank" rel="noopener">github-homebrewService</a></p>
<h2 id="cakebrew"><a href="#cakebrew" class="headerlink" title="cakebrew"></a>cakebrew</h2><hr>
<p>。这是一个图形化的 homebrew 工具，<a href="https://www.cakebrew.com/" target="_blank" rel="noopener">官网</a><br>。可以用来可视化的安装，卸载，管理 package 等<br><img src="http://slblogimg.oss-cn-beijing.aliyuncs.com/images/20160429/cakebrew.png" alt=""></p>
<h2 id="Reference"><a href="#Reference" class="headerlink" title="Reference"></a>Reference</h2><hr>
<p><a href="https://www.quora.com/How-does-Homebrew-work-internally" target="_blank" rel="noopener">How does Homebrew work internally?</a><br><a href="https://www.quora.com/What-is-the-history-of-Homebrew" target="_blank" rel="noopener">What is the history of Homebrew?</a><br><a href="https://icyleaf.com/2014/01/homebrew-hidden-commands/" target="_blank" rel="noopener">Homebrew 隐藏命令</a><br><a href="http://dghubble.com/blog/posts/homebrew-os-x-package-management/" target="_blank" rel="noopener">Homebrew: OS X Package Management</a></p>

      
    </div>
    <footer class="article-footer">
      
        <a data-url="http://caltyang.cn/2016/04/29/Homebrew是如何工作的/" data-id="cjnlj8fb30003h9jbowiuukbx" class="article-share-link">Share</a>
      

      

      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/OSX/">OSX</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2016/09/01/Maven中一些技巧/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Maven中一些技巧
        
      </div>
    </a>
  
  
    <a href="/2016/03/01/SpringBoot中配置CORS/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">SpringBoot中配置CORS</div>
    </a>
  
</nav>

  
</article>

</section>
      
      <aside id="sidebar">
  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Java/">Java</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/">Linux</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Maven/">Maven</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/OSX/">OSX</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Spring/">Spring</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/工具/">工具</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/数据库/">数据库</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/架构/">架构</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/网络/">网络</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/设计/">设计</a><span class="tag-list-count">1</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Java/" style="font-size: 20px;">Java</a> <a href="/tags/Linux/" style="font-size: 10px;">Linux</a> <a href="/tags/Maven/" style="font-size: 10px;">Maven</a> <a href="/tags/OSX/" style="font-size: 13.33px;">OSX</a> <a href="/tags/Spring/" style="font-size: 10px;">Spring</a> <a href="/tags/linux/" style="font-size: 16.67px;">linux</a> <a href="/tags/工具/" style="font-size: 13.33px;">工具</a> <a href="/tags/数据库/" style="font-size: 10px;">数据库</a> <a href="/tags/架构/" style="font-size: 10px;">架构</a> <a href="/tags/网络/" style="font-size: 10px;">网络</a> <a href="/tags/设计/" style="font-size: 10px;">设计</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">October 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/06/">June 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/">January 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">December 2017</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/">March 2017</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/02/">February 2017</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/12/">December 2016</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/10/">October 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/09/">September 2016</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/04/">April 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/03/">March 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/01/">January 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/08/">August 2015</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/12/">December 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/09/">September 2014</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2013/06/">June 2013</a><span class="archive-list-count">1</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2018/10/23/使用SSH隧道和内网渗透访问家中服务器/">使用SSH隧道和内网渗透访问家中服务器</a>
          </li>
        
          <li>
            <a href="/2018/06/11/单例的几种写法/">单例的几种写法</a>
          </li>
        
          <li>
            <a href="/2018/01/14/记OSX中遇到的一次Finder文件灰化的问题/">记OSX中遇到的一次Finder文件灰化的问题</a>
          </li>
        
          <li>
            <a href="/2017/12/23/记一次解决CLOSE-WAIT/">记一次解决CLOSE_WAIT</a>
          </li>
        
          <li>
            <a href="/2017/12/04/用Nginx进行反向代理/">用Nginx进行反向代理</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
      
    </div>
    <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2018 Shawn Liu<br>
      Powered by <a href="//hexo.io/" target="_blank">Hexo</a>
      .
      Theme by <a href="https://github.com/xiangming/landscape-plus" target="_blank">Landscape-plus</a>
    </div>
  </div>
</footer>
  </div>
  <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
  <!-- totop start -->
<div id="totop">
<a title="totop"><img src="/img/scrollup.png"/></a>
</div>

<!-- totop end -->


<!-- 百度分享 start -->

<!-- 百度分享 end -->

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>




<script src="/js/script.js"></script>

</div>
</body>
</html>
